/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.OvBillBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.DepartmentDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.OvBillDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.CodeRule;
import com.minierp.model.OvBill;
import com.minierp.model.TallySet;
import com.minierp.service.OvBillService;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

public class OvBillFormCtr extends SimpleFormController {
	private DepartmentDao departmentDao;
	private OvBillDao ovBillDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private IdGenerator idGenerator;
	private OvBillService ovBillService;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		String retVal = request.getParameter("ret_val");
		if (retVal == null) {
			retVal = "";
		}

		refData.put("retVal", retVal);
		refData.put("departmentList", this.departmentDao.getList(true));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("OV_BILL");
		if (cr == null) {
			throw new Exception("Code rule not found for ov bill");
		} else {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			OvBillBean form = new OvBillBean();
			form.setCodeRule(cr);
			if (id != null && !id.equals("")) {
				OvBill templateId1 = (OvBill) this.ovBillDao.get(OvBill.class,
						Long.parseLong(id));
				if (templateId1 == null) {
					throw new Exception("Record not found for id:" + id);
				}

				TallySet ovBill1 = (TallySet) this.ovBillDao.get(
						TallySet.class, templateId1.getTallySetId());
				form.setSychronizeValue(templateId1.getUpdateTime().getTime());
				if (ovBill1 != null) {
					form.setTallySetName(ovBill1.getTitle());
				}

				form.setOldId(templateId1.getId());
				form.setCostStr(templateId1.getCost().toString());
				form.setOutDateStr(sf.format(templateId1.getOutDate()));
				form.setOvBill(templateId1);
			} else {
				if (!EncCommonHelper.checkPrivilege(request, "OV_BILL")) {
					throw new PermissionDennyException("Permission Denny");
				}

				String templateId = request.getParameter("template_id");
				OvBill ovBill = new OvBill();
				UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
				if (templateId != null && !templateId.equals("")) {
					OvBill tf = (OvBill) this.ovBillDao.get(OvBill.class,
							Long.parseLong(templateId));
					if (tf == null) {
						throw new Exception("Template OvBill not found for id "
								+ templateId);
					}

					ovBill.setStafferId(tf.getStafferId());
					ovBill.setStafferName(tf.getStafferName());
					ovBill.setDepId(tf.getDepId());
					ovBill.setOutDate(tf.getOutDate());
					ovBill.setOutType(tf.getOutType());
					ovBill.setDescription(tf.getDescription());
					ovBill.setMark(tf.getMark());
				} else {
					ovBill.setStafferId(ub.getStaffer().getId());
					ovBill.setStafferName(ub.getStaffer().getStafferName());
					ovBill.setDepId(ub.getStaffer().getDepartmentId());
				}

				ovBill.setCreaterId(ub.getStaffer().getId());
				ovBill.setCreaterName(ub.getStaffer().getStafferName());
				ovBill.setTallySetId(ub.getUser().getDfTsId());
				form.setTallySetName(ub.getDfTsName());
				form.setOutDateStr(sf.format(ovBill.getOutDate()));
				form.setOvBill(ovBill);
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String id = request.getParameter("id");
		String examine = request.getParameter("examine");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		OvBillBean form = (OvBillBean) command;
		OvBill obj = form.getOvBill();
		if (examine != null && examine.equals("1")) {
			if (!EncCommonHelper.checkPrivilege(request, "OV_EXAM")) {
				throw new Exception("Permission deny!");
			}

			obj.setExamined("Y");
			if (ub.getStaffer() != null) {
				obj.setExaminerId(ub.getStaffer().getId());
				obj.setExaminerName(ub.getStaffer().getStafferName());
			}
		} else {
			obj.setExamined("N");
		}

		if (id != null && !id.equals("")) {
			if (!EncCommonHelper.checkPrivilege(request, "OV_BILL")) {
				throw new Exception("Permission deny!");
			}

			try {
				this.ovBillService.txUpdate(form, this.ovBillDao,
						this.idGenerator);
			} catch (CannotAcquireLockException arg12) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			}
		} else {
			try {
				this.ovBillService.txInsert(form, this.ovBillDao,
						this.serialNumberDao, this.idGenerator);
			} catch (CannotAcquireLockException arg13) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			}
		}

		mv.addObject("returnValue", String.valueOf(obj.getId()));
		String continueFlag = request.getParameter("continue_flag");
		String alertType = request.getParameter("alert_type");
		if (continueFlag != null && !continueFlag.equals("")) {
			if (continueFlag.equals("NEW")) {
				EncCommonHelper.setAlertMessage(request, "产值单保存成功！");
				mv = new ModelAndView(
						"redirect:/make/OvBillNew.ihtm?ret_val=1&alert_type="
								+ alertType);
			} else if (continueFlag.equals("COPY")) {
				EncCommonHelper.setAlertMessage(request, "产值单保存成功！");
				mv = new ModelAndView(
						"redirect:/make/OvBillNew.ihtm?ret_val=1&template_id="
								+ obj.getId() + "&alert_type=" + alertType);
			}
		} else if (alertType == null || !alertType.equals("NONE")) {
			EncCommonHelper.setAlertMessage(request, "产值单保存成功！");
			if (alertType != null && alertType.equals("FORM")) {
				mv = new ModelAndView(new RedirectView(
						"/make/OvBillEdit.ihtm?ret_val=1&id=" + obj.getId()
								+ "&alert_type=" + alertType));
			}
		}

		mv.addObject("alertType", alertType);
		return mv;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public DepartmentDao getDepartmentDao() {
		return this.departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public OvBillDao getOvBillDao() {
		return this.ovBillDao;
	}

	public void setOvBillDao(OvBillDao ovBillDao) {
		this.ovBillDao = ovBillDao;
	}

	public OvBillService getOvBillService() {
		return this.ovBillService;
	}

	public void setOvBillService(OvBillService ovBillService) {
		this.ovBillService = ovBillService;
	}
}